using System.Collections.Generic;
using UnityEngine;

namespace LCH
{
    /// <summary>
    /// 控制图操作辅助方法 - 提供控制图数组的统一操作接口
    /// </summary>
    public static class ControlTextureOperations
    {
        /// <summary>
        /// 初始化控制图数组
        /// </summary>
        public static void InitializeControlTextures(int count, out Texture2D[] ctrlText, out RenderTexture[] editorCtrl1RT)
        {
            ctrlText = new Texture2D[count];
            editorCtrl1RT = new RenderTexture[count];
            
            for (int i = 0; i < count; i++)
            {
                ctrlText[i] = null;
                editorCtrl1RT[i] = null;
            }
        }
        
        /// <summary>
        /// 确保控制图数组大小正确
        /// </summary>
        public static void EnsureControlTextureArrays(int requiredCount, ref Texture2D[] ctrlText, ref RenderTexture[] editorCtrl1RT)
        {
            // 确保 ctrlText 数组大小正确
            if (ctrlText == null || ctrlText.Length != requiredCount)
            {
                // 释放旧的数组（如果需要）
                if (ctrlText != null)
                {
                    for (int i = 0; i < ctrlText.Length; i++)
                    {
                        ctrlText[i] = null;
                    }
                }
                ctrlText = new Texture2D[requiredCount];
                for (int i = 0; i < requiredCount; i++)
                {
                    ctrlText[i] = null;
                }
            }
            
            // 确保 editorCtrl1RT 数组大小正确
            if (editorCtrl1RT == null || editorCtrl1RT.Length != requiredCount)
            {
                // 释放旧的RenderTexture
                if (editorCtrl1RT != null)
                {
                    for (int i = 0; i < editorCtrl1RT.Length; i++)
                    {
                        if (editorCtrl1RT[i] != null)
                            GameObject.DestroyImmediate(editorCtrl1RT[i], true);
                    }
                }
                editorCtrl1RT = new RenderTexture[requiredCount];
                for (int i = 0; i < requiredCount; i++)
                {
                    editorCtrl1RT[i] = null;
                }
            }
        }
        
        /// <summary>
        /// 释放所有控制图资源
        /// </summary>
        public static void ReleaseControlTextures(ref Texture2D[] ctrlText, ref RenderTexture[] editorCtrl1RT)
        {
            // 清空 Texture2D 引用
            if (ctrlText != null)
            {
                for (int i = 0; i < ctrlText.Length; i++)
                {
                    ctrlText[i] = null;
                }
                ctrlText = null;
            }
            
            // 释放 RenderTexture
            if (editorCtrl1RT != null)
            {
                for (int i = 0; i < editorCtrl1RT.Length; i++)
                {
                    if (editorCtrl1RT[i] != null)
                    {
                        GameObject.DestroyImmediate(editorCtrl1RT[i], true);
                        editorCtrl1RT[i] = null;
                    }
                }
                editorCtrl1RT = null;
            }
        }
        
        /// <summary>
        /// 清空控制图数据（不释放资源）
        /// </summary>
        public static void ClearControlTextures(Texture2D[] ctrlText, RenderTexture[] editorCtrl1RT)
        {
            if (ctrlText != null)
            {
                for (int i = 0; i < ctrlText.Length; i++)
                {
                    ctrlText[i] = null;
                }
            }
            
            if (editorCtrl1RT != null)
            {
                for (int i = 0; i < editorCtrl1RT.Length; i++)
                {
                    if (editorCtrl1RT[i] != null)
                    {
                        GameObject.DestroyImmediate(editorCtrl1RT[i], true);
                        editorCtrl1RT[i] = null;
                    }
                }
            }
        }
        
        /// <summary>
        /// 将控制图应用到材质
        /// </summary>
        public static void ApplyControlTexturesToMaterial(Material material, Texture2D[] ctrlText, List<string> controlTextureNames)
        {
            if (material == null || ctrlText == null || controlTextureNames == null)
                return;
            
            for (int i = 0; i < controlTextureNames.Count && i < ctrlText.Length; i++)
            {
                if (ctrlText[i] != null)
                {
                    material.SetTexture(controlTextureNames[i], ctrlText[i]);
                }
            }
        }
        
        /// <summary>
        /// 验证控制图索引是否有效
        /// </summary>
        public static bool IsValidControlTextureIndex(int index, int controlTextureCount, Texture2D[] ctrlText = null, RenderTexture[] editorCtrl1RT = null)
        {
            if (index < 0 || index >= controlTextureCount)
                return false;
            
            if (ctrlText != null && (index >= ctrlText.Length))
                return false;
            
            if (editorCtrl1RT != null && (index >= editorCtrl1RT.Length))
                return false;
            
            return true;
        }
        
        /// <summary>
        /// 从材质重放控制图
        /// </summary>
        public static void ReplayControlTextureFromMaterial(Material material, int index, List<string> controlTextureNames, ref Texture2D[] ctrlText)
        {
            if (material == null || !IsValidControlTextureIndex(index, controlTextureNames.Count, ctrlText))
                return;
            
            if (index >= controlTextureNames.Count)
                return;
            
            string ctrlName = controlTextureNames[index];
            Texture t = material.GetTexture(ctrlName);
            
            if (t is Texture2D && ctrlText != null && index < ctrlText.Length)
            {
                ctrlText[index] = (Texture2D)t;
            }
        }
        
        /// <summary>
        /// 重放所有控制图
        /// </summary>
        public static void ReplayAllControlTextures(Material material, List<string> controlTextureNames, ref Texture2D[] ctrlText)
        {
            if (material == null || controlTextureNames == null)
                return;
            
            for (int i = 0; i < controlTextureNames.Count; i++)
            {
                ReplayControlTextureFromMaterial(material, i, controlTextureNames, ref ctrlText);
            }
        }
    }
}

